package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformFReport;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.domain.vo.PlatformFReportVO;
import com.ruoyi.teaching.mapper.PlatformFReportMapper;
import com.ruoyi.teaching.service.PlatformFReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2025-02-11
 */
@Service
public class PlatformFReportServiceImpl extends ServiceImpl<PlatformFReportMapper, PlatformFReport> implements PlatformFReportService {
    @Autowired
    private PlatformFReportMapper platformFReportMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     *
     * @param pageNum       第几页
     * @param pageSize      一页几条
     * @param reportTitle   标题
     * @param reportStatus  举报状态
     * @param reportType    举报类型
     * @param reportTime    提交时间 （0时间不限1一日内2一周内3一个月内4三个月内5半年内6一年内）
     * @return
     */
    @Override
    public AjaxResult allReport(Integer pageNum, Integer pageSize, String reportTitle, Integer reportStatus, Integer reportType, Integer reportTime) {
        Page<PlatformFReportVO> reportVOPage = new Page<>(pageNum, pageSize);
        LocalDateTime startDate = UniversalUtil.getStartDate(reportTime);
        Page<PlatformFReportVO> reportVOListPage = platformFReportMapper.selectPageAll(reportVOPage, reportTitle, reportStatus, reportType, startDate);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", reportVOListPage);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult dealReport(List<PlatformFReport> reports) {
        int i = 0;
        for (PlatformFReport report : reports) {
            UniversalCreateUpdate updateInfo = UniversalUtil.getCreateUpdate(sysUserMapper);
            report.setUpdateBy(updateInfo.getName());
            report.setUpdateTime(DateUtils.getNowDate());
            report.setUpdateId(updateInfo.getId());
            i += platformFReportMapper.updateById(report);
        }
        if (i == reports.size()) {
            return new AjaxResult(HttpStatus.SUCCESS, "更改成功");
        } else {
            throw new RuntimeException("更改失败");
        }
    }

    @Override
    public AjaxResult addReport(PlatformFReport platformFReport) {
        UniversalCreateUpdate createInfo = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformFReport.setCreateBy(createInfo.getName());
        platformFReport.setCreateTime(DateUtils.getNowDate());
        platformFReport.setCreateId(createInfo.getId());
        int insert = platformFReportMapper.insert(platformFReport);
        if(insert > 0){
            return new AjaxResult(HttpStatus.SUCCESS,"添加成功");
        }
        return new AjaxResult(HttpStatus.ERROR,"添加失败");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult deleteReportByIds(Long[] ids) {
        int i = platformFReportMapper.deleteBatchIds(Arrays.asList(ids));
        if (i == ids.length) {
            return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
        } else {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public AjaxResult reportDetail(Long id) {
        PlatformFReport platformFReport = platformFReportMapper.selectById(id);
        PlatformFReportVO platformFReportVO = new PlatformFReportVO();
        BeanUtils.copyProperties(platformFReport,platformFReportVO);
        String[] split = platformFReport.getReportImg().split(",");
        List<String> imgList = new ArrayList<>(Arrays.asList(split));
        platformFReportVO.setReportImg(imgList);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", platformFReport);
    }
}
